def dfs(x: int, y: int, point: int):
    global board_copy, visited, maxPoint, diretion
    # print("x,y:", x, y, "point:", point)
    for e in diretion:
        qx = x + e[0]
        qy = y + e[1]
        xx = x + 2 * e[0]
        yy = y + 2 * e[1]
        if not check(xx, yy): continue
        if board_copy[qx][qy] == 'Q':
            board_copy[qx][qy] = '.'
            board_copy[x][y] = '.'
            board_copy[xx][yy] = 'L'
            point += 1
            dfs(xx, yy, point)
            point -= 1
            board_copy[qx][qy] = 'Q'
            board_copy[x][y] = 'L'
            board_copy[xx][yy] = '.'
    maxPoint = max(point, maxPoint)


def check(x: int, y: int):
    global n, board_copy
    if x < 1 or y < 1 or x > n or y > n: return False
    if board_copy[x][y] != '.': return False
    return True


diretion = [[-1, -1], [-1, 1], [1, 1], [1, -1]]
n = int(input())
board = [['0'] * (n + 1) for i in range(n + 1)]
visited = [[False] * (n + 1) for j in range(n + 1)]
index = []
for i in range(1, n + 1):
    line = list(input())
    for j in range(1, n + 1):
        board[i][j] = line[j - 1]
        if board[i][j] == 'L': index.append((i, j))
maxPoint = 0
# for e in board:
#     print(e)
for e in index:
    board_copy = board.copy()
    dfs(e[0], e[1], 0)
print(maxPoint)
